package com.iot.ammeter.controller.provider;


import com.iot.ammeter.anno.LogAnnotation;
import com.iot.ammeter.dto.web.provider.ProviderDTO;
import com.iot.ammeter.dto.web.provider.ProviderIdNameDTO;
import com.iot.ammeter.responseResult.ResponseResult;
import com.iot.ammeter.service.web.provider.ProviderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 属服务商，组织 前端控制器
 * </p>
 *
 * @author basil
 * @since 2021-09-25
 */
@RestController
@Api(tags = {"服务商管理"})
@RequestMapping("/api/provider")
public class ProviderController {

    private final ProviderService providerService;

    public ProviderController(ProviderService providerService) {
        this.providerService = providerService;
    }

    /**
     * 获取所有的服务商
     *
     * @return 返回所有的服务商json数据
     */
    @ApiOperation("获取所有的服务商")
    @LogAnnotation(module = "getProviderList", operator = "获取所有的服务商")
    @GetMapping("/list")
    public ResponseResult getProviderList() {
        List<ProviderDTO> providerList = providerService.getProviderList();
        return providerList.size() > 0 ? ResponseResult.SUCCESS(providerList) : ResponseResult.FAILED("无数据");
    }

    /**
     * 添加服务商
     *
     * @param providerDto 服务商信息
     * @return 添加成功或失败提示
     */
    @ApiOperation("添加服务商")
    @LogAnnotation(module = "addProvider", operator = "添加服务商")
    @PostMapping("/adjuration")
    public ResponseResult addProvider(@RequestBody ProviderDTO providerDto) {
        String tips = providerService.addProvider(providerDto);
        return "添加服务商成功".equals(tips) ? ResponseResult.SUCCESS(tips) : ResponseResult.FAILED(tips);
    }

    /**
     * 删除服务商
     *
     * @param providerIds 服务商id
     * @return ResponseResult
     */
    @ApiOperation("删除服务商")
    @LogAnnotation(module = "deleteProvider", operator = "删除服务商")
    @DeleteMapping("/delete")
    public ResponseResult deleteProvider(@RequestBody List<String> providerIds) {
        String tips = providerService.deleteProvider(providerIds);
        return "删除成功。".equals(tips) ? ResponseResult.SUCCESS(tips) : ResponseResult.FAILED(tips);
    }

    /**
     * 获取服务商id和名称
     *
     * @return 服务商id和名称集合
     */
    @ApiOperation("获取服务商id和名称")
    @LogAnnotation(module = "getProviderIdAndName", operator = "获取服务商id和名称")
    @GetMapping("/name_id")
    public ResponseResult getProviderIdAndName() {
        List<ProviderIdNameDTO> providerIdAndName = providerService.getProviderIdAndName();
        return providerIdAndName == null ? ResponseResult.FAILED("无数据") : ResponseResult.SUCCESS(providerIdAndName);
    }
}
